package com.neu.cpspgateway.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.cpspgateway.pojo.AuthorityUser;
import com.neu.cpspgateway.service.AuthorityService;
import com.neu.cpspgateway.util.result.Result;
import com.neu.cpspgateway.util.result.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RequestMapping("/authority")
@RestController
public class AuthorityController {

    @Autowired
    AuthorityService authorityService;

    @ApiOperation("cha")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result selectAuthorityAndUserByUserId(Integer userId){
        return ResultGenerator.genSuccessResult(authorityService.selectAuthorityAndUserByUserId(userId));
    }

    @ApiOperation("按sub查找")
    @RequestMapping(value = "/sub",method = RequestMethod.POST)
    @ResponseBody
    public Result getAuthorityInfoBySubSystemId(@RequestParam Integer subSystemId,
                                                @RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<AuthorityUser> list = authorityService.selectAuthorityUserBySubSystemId(subSystemId);
        PageInfo pageInfo = PageInfo.of(list);
        if(list!= null){
            return ResultGenerator.genSuccessResult(pageInfo);
        }else {
            return  ResultGenerator.genFailResult("找不到该内容");
        }

    }

    @ApiOperation("按name查找")
    @RequestMapping(value = "/name",method = RequestMethod.POST)
    @ResponseBody
    public Result getAuthorityInfoByNameAndSubSystemId(@RequestParam String name,@RequestParam Integer subSystemId){
        List<AuthorityUser> a= new ArrayList<AuthorityUser>();
         a.addAll(authorityService.selectAuthorityUserByNameAndSubSystemId(name, subSystemId));

        if (a.get(0)!=null){
            return ResultGenerator.genSuccessResult(a);
        }else {
            return  ResultGenerator.genFailResult("用户不存在");
        }

    }

    @ApiOperation("查看")
    @RequestMapping(value = "/updateLookAuthority",method = RequestMethod.POST)
    @ResponseBody
    public Result updateLookAuthorityByUserId(@RequestParam Integer userId,@RequestParam Boolean lookAuthority){
        if(authorityService.updateLookAuthorityByUserId(userId,lookAuthority) == 1){
            return ResultGenerator.genSuccessResult("修改成功");
        }else{
            return ResultGenerator.genFailResult("修改失败");
        }

    }

    @ApiOperation("查看")
    @RequestMapping(value = "/updateEditAuthority",method = RequestMethod.POST)
    @ResponseBody
    public Result updateEditAuthorityByUserId(@RequestParam Integer userId,@RequestParam Boolean editAuthority){
        if(authorityService.updateEditAuthorityByUserId(userId,editAuthority) == 1){
            return ResultGenerator.genSuccessResult("修改成功");
        }else{
            return ResultGenerator.genFailResult("修改失败");
        }

    }
}
